package com.estun.backroundManagementService.config;

import com.estun.backroundManagementService.config.properties.FtpProperties;
import com.estun.backroundManagementService.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

//    @Value("${spring.profiles.active:123}")
//    private String activeProfile;

    @Autowired
    private FtpProperties ftpProperties;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //获取jar包所在目录
        ApplicationHome h = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = h.getDir().getPath() + File.separator + "uploadFile" + File.separator;
//        String dirPath = FileUtils.savePath;
        log.info("映射文件路径为dirPath:{}", dirPath);

        log.info("实际映射路径为:{}", "file:" + FileUtil.savePath);
        registry.addResourceHandler(FileUtil.basePath + "**").addResourceLocations("file:" + dirPath);
//        registry.addResourceHandler(FileUtils.basePath + "**").addResourceLocations("file:" + FileUtils.savePath);
        registry.addResourceHandler("/app/Program/**").addResourceLocations("file:" + ftpProperties.getDownloadPath() + File.separator);
    }

    //生产环境不需要
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //非dev环境不启动服务跨域
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }
}
